.TH "TCADB" 3 "2012-08-18" "Man Page" "Tokyo Cabinet"

.SH NAME
tcadb \- the abstract database API

.SH DESCRIPTION
.PP
Abstract database is a set of interfaces to use on\-memory hash database, on\-memory tree database, hash database, B+ tree database, fixed\-length database, and table database with the same API.
.PP
To use the abstract database API, include `\fBtcutil.h\fR', `\fBtcadb.h\fR', and related standard header files.  Usually, write the following description near the front of a source file.
.PP
.RS
.br
\fB#include <tcutil.h>\fR
.br
\fB#include <tcadb.h>\fR
.br
\fB#include <stdlib.h>\fR
.br
\fB#include <stdbool.h>\fR
.br
\fB#include <stdint.h>\fR
.RE
.PP
Objects whose type is pointer to `\fBTCADB\fR' are used to handle abstract databases.  An abstract database object is created with the function `\fBtcadbnew\fR' and is deleted with the function `\fBtcadbdel\fR'.  To avoid memory leak, it is important to delete every object when it is no longer in use.
.PP
Before operations to store or retrieve records, it is necessary to connect the abstract database object to the concrete one.  The function `\fBtcadbopen\fR' is used to open a concrete database and the function `\fBtcadbclose\fR' is used to close the database.  To avoid data missing or corruption, it is important to close every database instance when it is no longer in use.  It is forbidden for multible database objects in a process to open the same database at the same time.

.SH API
.PP
The function `tcadbnew' is used in order to create an abstract database object.
.PP
.RS
.br
\fBTCADB *tcadbnew(void);\fR
.RS
The return value is the new abstract database object.
.RE
.RE
.PP
The function `tcadbdel' is used in order to delete an abstract database object.
.PP
.RS
.br
\fBvoid tcadbdel(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RE
.PP
The function `tcadbopen' is used in order to open an abstract database.
.PP
.RS
.br
\fBbool tcadbopen(TCADB *\fIadb\fB, const char *\fIname\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIname\fR' specifies the name of the database.  If it is "*", the database will be an on\-memory hash database.  If it is "+", the database will be an on\-memory tree database.  If its suffix is ".tch", the database will be a hash database.  If its suffix is ".tcb", the database will be a B+ tree database.  If its suffix is ".tcf", the database will be a fixed\-length database.  If its suffix is ".tct", the database will be a table database.  Otherwise, this function fails.  Tuning parameters can trail the name, separated by "#".  Each parameter is composed of the name and the value, separated by "=".  On\-memory hash database supports "bnum", "capnum", and "capsiz".  On\-memory tree database supports "capnum" and "capsiz".  Hash database supports "mode", "bnum", "apow", "fpow", "opts", "rcnum", "xmsiz", and "dfunit".  B+ tree database supports "mode", "lmemb", "nmemb", "bnum", "apow", "fpow", "opts", "lcnum", "ncnum", "xmsiz", and "dfunit".  Fixed\-length database supports "mode", "width", and "limsiz".  Table database supports "mode", "bnum", "apow", "fpow", "opts", "rcnum", "lcnum", "ncnum", "xmsiz", "dfunit", and "idx".
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
The tuning parameter "capnum" specifies the capacity number of records.  "capsiz" specifies the capacity size of using memory.  Records spilled the capacity are removed by the storing order.  "mode" can contain "w" of writer, "r" of reader, "c" of creating, "t" of truncating, "e" of no locking, and "f" of non\-blocking lock.  The default mode is relevant to "wc".  "opts" can contains "l" of large option, "d" of Deflate option, "b" of BZIP2 option, and "t" of TCBS option.  "idx" specifies the column name of an index and its type separated by ":".  For example, "casket.tch#bnum=1000000#opts=ld" means that the name of the database file is "casket.tch", and the bucket number is 1000000, and the options are large and Deflate.
.RE
.RE
.PP
The function `tcadbclose' is used in order to close an abstract database object.
.PP
.RS
.br
\fBbool tcadbclose(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
Update of a database is assured to be written when the database is closed.  If a writer opens a database but does not close it appropriately, the database will be broken.
.RE
.RE
.PP
The function `tcadbput' is used in order to store a record into an abstract database object.
.PP
.RS
.br
\fBbool tcadbput(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, const void *\fIvbuf\fB, int \fIvsiz\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fIvbuf\fR' specifies the pointer to the region of the value.
.RE
.RS
`\fIvsiz\fR' specifies the size of the region of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If a record with the same key exists in the database, it is overwritten.
.RE
.RE
.PP
The function `tcadbput2' is used in order to store a string record into an abstract object.
.PP
.RS
.br
\fBbool tcadbput2(TCADB *\fIadb\fB, const char *\fIkstr\fB, const char *\fIvstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
`\fIvstr\fR' specifies the string of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If a record with the same key exists in the database, it is overwritten.
.RE
.RE
.PP
The function `tcadbputkeep' is used in order to store a new record into an abstract database object.
.PP
.RS
.br
\fBbool tcadbputkeep(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, const void *\fIvbuf\fB, int \fIvsiz\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fIvbuf\fR' specifies the pointer to the region of the value.
.RE
.RS
`\fIvsiz\fR' specifies the size of the region of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If a record with the same key exists in the database, this function has no effect.
.RE
.RE
.PP
The function `tcadbputkeep2' is used in order to store a new string record into an abstract database object.
.PP
.RS
.br
\fBbool tcadbputkeep2(TCADB *\fIadb\fB, const char *\fIkstr\fB, const char *\fIvstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
`\fIvstr\fR' specifies the string of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If a record with the same key exists in the database, this function has no effect.
.RE
.RE
.PP
The function `tcadbputcat' is used in order to concatenate a value at the end of the existing record in an abstract database object.
.PP
.RS
.br
\fBbool tcadbputcat(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, const void *\fIvbuf\fB, int \fIvsiz\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fIvbuf\fR' specifies the pointer to the region of the value.
.RE
.RS
`\fIvsiz\fR' specifies the size of the region of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If there is no corresponding record, a new record is created.
.RE
.RE
.PP
The function `tcadbputcat2' is used in order to concatenate a string value at the end of the existing record in an abstract database object.
.PP
.RS
.br
\fBbool tcadbputcat2(TCADB *\fIadb\fB, const char *\fIkstr\fB, const char *\fIvstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
`\fIvstr\fR' specifies the string of the value.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
If there is no corresponding record, a new record is created.
.RE
.RE
.PP
The function `tcadbout' is used in order to remove a record of an abstract database object.
.PP
.RS
.br
\fBbool tcadbout(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RE
.PP
The function `tcadbout2' is used in order to remove a string record of an abstract database object.
.PP
.RS
.br
\fBbool tcadbout2(TCADB *\fIadb\fB, const char *\fIkstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RE
.PP
The function `tcadbget' is used in order to retrieve a record in an abstract database object.
.PP
.RS
.br
\fBvoid *tcadbget(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, int *\fIsp\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fIsp\fR' specifies the pointer to the variable into which the size of the region of the return value is assigned.
.RE
.RS
If successful, the return value is the pointer to the region of the value of the corresponding record.  `NULL' is returned if no record corresponds.
.RE
.RS
Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string.  Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use.
.RE
.RE
.PP
The function `tcadbget2' is used in order to retrieve a string record in an abstract database object.
.PP
.RS
.br
\fBchar *tcadbget2(TCADB *\fIadb\fB, const char *\fIkstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
If successful, the return value is the string of the value of the corresponding record.  `NULL' is returned if no record corresponds.
.RE
.RS
Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use.
.RE
.RE
.PP
The function `tcadbvsiz' is used in order to get the size of the value of a record in an abstract database object.
.PP
.RS
.br
\fBint tcadbvsiz(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
If successful, the return value is the size of the value of the corresponding record, else, it is \-1.
.RE
.RE
.PP
The function `tcadbvsiz2' is used in order to get the size of the value of a string record in an abstract database object.
.PP
.RS
.br
\fBint tcadbvsiz2(TCADB *\fIadb\fB, const char *\fIkstr\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkstr\fR' specifies the string of the key.
.RE
.RS
If successful, the return value is the size of the value of the corresponding record, else, it is \-1.
.RE
.RE
.PP
The function `tcadbiterinit' is used in order to initialize the iterator of an abstract database object.
.PP
.RS
.br
\fBbool tcadbiterinit(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
The iterator is used in order to access the key of every record stored in a database.
.RE
.RE
.PP
The function `tcadbiternext' is used in order to get the next key of the iterator of an abstract database object.
.PP
.RS
.br
\fBvoid *tcadbiternext(TCADB *\fIadb\fB, int *\fIsp\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIsp\fR' specifies the pointer to the variable into which the size of the region of the return value is assigned.
.RE
.RS
If successful, the return value is the pointer to the region of the next key, else, it is `NULL'.  `NULL' is returned when no record is to be get out of the iterator.
.RE
.RS
Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string.  Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use.  It is possible to access every record by iteration of calling this function.  It is allowed to update or remove records whose keys are fetched while the iteration.  However, it is not assured if updating the database is occurred while the iteration.  Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access.
.RE
.RE
.PP
The function `tcadbiternext2' is used in order to get the next key string of the iterator of an abstract database object.
.PP
.RS
.br
\fBchar *tcadbiternext2(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is the string of the next key, else, it is `NULL'.  `NULL' is returned when no record is to be get out of the iterator.
.RE
.RS
Because the region of the return value is allocated with the `malloc' call, it should be released with the `free' call when it is no longer in use.  It is possible to access every record by iteration of calling this function.  However, it is not assured if updating the database is occurred while the iteration.  Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access.
.RE
.RE
.PP
The function `tcadbfwmkeys' is used in order to get forward matching keys in an abstract database object.
.PP
.RS
.br
\fBTCLIST *tcadbfwmkeys(TCADB *\fIadb\fB, const void *\fIpbuf\fB, int \fIpsiz\fB, int \fImax\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIpbuf\fR' specifies the pointer to the region of the prefix.
.RE
.RS
`\fIpsiz\fR' specifies the size of the region of the prefix.
.RE
.RS
`\fImax\fR' specifies the maximum number of keys to be fetched.  If it is negative, no limit is specified.
.RE
.RS
The return value is a list object of the corresponding keys.  This function does never fail.  It returns an empty list even if no key corresponds.
.RE
.RS
Because the object of the return value is created with the function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer in use.  Note that this function may be very slow because every key in the database is scanned.
.RE
.RE
.PP
The function `tcadbfwmkeys2' is used in order to get forward matching string keys in an abstract database object.
.PP
.RS
.br
\fBTCLIST *tcadbfwmkeys2(TCADB *\fIadb\fB, const char *\fIpstr\fB, int \fImax\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIpstr\fR' specifies the string of the prefix.
.RE
.RS
`\fImax\fR' specifies the maximum number of keys to be fetched.  If it is negative, no limit is specified.
.RE
.RS
The return value is a list object of the corresponding keys.  This function does never fail.  It returns an empty list even if no key corresponds.
.RE
.RS
Because the object of the return value is created with the function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer in use.  Note that this function may be very slow because every key in the database is scanned.
.RE
.RE
.PP
The function `tcadbaddint' is used in order to add an integer to a record in an abstract database object.
.PP
.RS
.br
\fBint tcadbaddint(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, int \fInum\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fInum\fR' specifies the additional value.
.RE
.RS
If successful, the return value is the summation value, else, it is `INT_MIN'.
.RE
.RS
If the corresponding record exists, the value is treated as an integer and is added to.  If no record corresponds, a new record of the additional value is stored.
.RE
.RE
.PP
The function `tcadbadddouble' is used in order to add a real number to a record in an abstract database object.
.PP
.RS
.br
\fBdouble tcadbadddouble(TCADB *\fIadb\fB, const void *\fIkbuf\fB, int \fIksiz\fB, double \fInum\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIkbuf\fR' specifies the pointer to the region of the key.
.RE
.RS
`\fIksiz\fR' specifies the size of the region of the key.
.RE
.RS
`\fInum\fR' specifies the additional value.
.RE
.RS
If successful, the return value is the summation value, else, it is Not-a-Number.
.RE
.RS
If the corresponding record exists, the value is treated as a real number and is added to.  If no record corresponds, a new record of the additional value is stored.
.RE
.RE
.PP
The function `tcadbsync' is used in order to synchronize updated contents of an abstract database object with the file and the device.
.PP
.RS
.br
\fBbool tcadbsync(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RE
.PP
The function `tcadboptimize' is used in order to optimize the storage of an abstract database object.
.PP
.RS
.br
\fBbool tcadboptimize(TCADB *\fIadb\fB, const char *\fIparams\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIparams\fR' specifies the string of the tuning parameters, which works as with the tuning of parameters the function `tcadbopen'.  If it is `NULL', it is not used.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
This function is useful to reduce the size of the database storage with data fragmentation by successive updating.
.RE
.RE
.PP
The function `tcadbvanish' is used in order to remove all records of an abstract database object.
.PP
.RS
.br
\fBbool tcadbvanish(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RE
.PP
The function `tcadbcopy' is used in order to copy the database file of an abstract database object.
.PP
.RS
.br
\fBbool tcadbcopy(TCADB *\fIadb\fB, const char *\fIpath\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIpath\fR' specifies the path of the destination file.  If it begins with `@', the trailing substring is executed as a command line.
.RE
.RS
If successful, the return value is true, else, it is false.  False is returned if the executed command returns non\-zero code.
.RE
.RS
The database file is assured to be kept synchronized and not modified while the copying or executing operation is in progress.  So, this function is useful to create a backup file of the database file.
.RE
.RE
.PP
The function `tcadbtranbegin' is used in order to begin the transaction of an abstract database object.
.PP
.RS
.br
\fBbool tcadbtranbegin(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
The database is locked by the thread while the transaction so that only one transaction can be activated with a database object at the same time.  Thus, the serializable isolation level is assumed if every database operation is performed in the transaction.  All updated regions are kept track of by write ahead logging while the transaction.  If the database is closed during transaction, the transaction is aborted implicitly.
.RE
.RE
.PP
The function `tcadbtrancommit' is used in order to commit the transaction of an abstract database object.
.PP
.RS
.br
\fBbool tcadbtrancommit(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
Update in the transaction is fixed when it is committed successfully.
.RE
.RE
.PP
The function `tcadbtranabort' is used in order to abort the transaction of an abstract database object.
.PP
.RS
.br
\fBbool tcadbtranabort(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
If successful, the return value is true, else, it is false.
.RE
.RS
Update in the transaction is discarded when it is aborted.  The state of the database is rollbacked to before transaction.
.RE
.RE
.PP
The function `tcadbpath' is used in order to get the file path of an abstract database object.
.PP
.RS
.br
\fBconst char *tcadbpath(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
The return value is the path of the database file or `NULL' if the object does not connect to any database.  "*" stands for on\-memory hash database.  "+" stands for on\-memory tree database.
.RE
.RE
.PP
The function `tcadbrnum' is used in order to get the number of records of an abstract database object.
.PP
.RS
.br
\fBuint64_t tcadbrnum(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
The return value is the number of records or 0 if the object does not connect to any database instance.
.RE
.RE
.PP
The function `tcadbsize' is used in order to get the size of the database of an abstract database object.
.PP
.RS
.br
\fBuint64_t tcadbsize(TCADB *\fIadb\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
The return value is the size of the database or 0 if the object does not connect to any database instance.
.RE
.RE
.PP
The function `tcadbmisc' is used in order to call a versatile function for miscellaneous operations of an abstract database object.
.PP
.RS
.br
\fBTCLIST *tcadbmisc(TCADB *\fIadb\fB, const char *\fIname\fB, const TCLIST *\fIargs\fB);\fR
.RS
`\fIadb\fR' specifies the abstract database object.
.RE
.RS
`\fIname\fR' specifies the name of the function.  All databases support "put", "out", "get", "putlist", "outlist", "getlist", and "getpart".  "put" is to store a record.  It receives a key and a value, and returns an empty list.  "out" is to remove a record.  It receives a key, and returns an empty list.  "get" is to retrieve a record.  It receives a key, and returns a list of the values.  "putlist" is to store records.  It receives keys and values one after the other, and returns an empty list.  "outlist" is to remove records.  It receives keys, and returns an empty list.  "getlist" is to retrieve records.  It receives keys, and returns keys and values of corresponding records one after the other.  "getpart" is to retrieve the partial value of a record.  It receives a key, the offset of the region, and the length of the region.
.RE
.RS
`\fIargs\fR' specifies a list object containing arguments.
.RE
.RS
If successful, the return value is a list object of the result.  `NULL' is returned on failure.
.RE
.RS
Because the object of the return value is created with the function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer in use.
.RE
.RE

.SH SEE ALSO
.PP
.BR tcatest (1),
.BR tcamttest (1),
.BR tcamgr (1),
.BR tokyocabinet (3)
